<script setup>
import {reactive, ref} from "vue";
import { useRoleStore } from "@/stores/roleStore.js";

const roleStore = useRoleStore();
const dialogVisible = ref(false)
const role = reactive({
  roleId: '',
  name:'',
  code:'',
  dataScope: '',
})
const rules = ref({
  name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入权限码', trigger: 'blur' }],
  dataScope: [{ required: true, message: '请选择权限类型', trigger: 'blur' }],
})
const roleFormRef = ref(null)

const submitForm = () => {
  roleFormRef.value?.validate().then(async () => {
    console.log('校验通过2222')
    await roleStore.updateRole({
      ...role
    })
    dialogVisible.value = false;
  }).catch(() => {
    console.log('校验失败')
  })
}

const show = (roleInfo) => {
  console.log('-------------show----------',roleInfo);
  // userFormRef.value?.resetFields();
  Object.assign(role, roleInfo);
  dialogVisible.value = true;
}
//对外暴露show
defineExpose({
  show
})

</script>

<template>
  <el-dialog
      title="编辑角色"
      v-model="dialogVisible"
      width="50%"
  >
    <el-form :model="role" :rules="rules" ref="roleFormRef" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="role.name" placeholder="请输入角色名称"></el-input>
      </el-form-item>
      <el-form-item label="权限码" prop="code">
        <el-input v-model="role.code" placeholder="请输入角色编码"></el-input>
      </el-form-item>
      <el-form-item label="数据权限范围" prop="dataScope">
        <el-select v-model="role.dataScope" placeholder="请选择数据权限范围">
          <el-option label="全部" value="1"></el-option>
          <el-option label="本部" value="2"></el-option>
          <el-option label="自定义" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button dataScope="primary" @click="submitForm">确定</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped>

</style>